﻿using LoveNote.Application.Share.Enums;
using LoveNote.Application.Share.Login;
using LoveNote.Application.Share.Message;
using LoveNote.Database.Entities;
using LoveNote.Database.Helpers;
using LoveNote.Database.Interfaces;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using TookKit.Mvvm.Attributes;
using TookKit.Mvvm.Bindings;
using TookKit.Mvvm.Enums;
using TookKit.Mvvm.Events;

namespace LoveNote.Application.Customer.ViewModels
{
    [DependencyInjection(Lifetime.Singleton)]
    internal class ProcessViewModel : ObservableObject
    {
        private CustomerEntity customer;
        public CustomerEntity Customer
        {
            get { return customer; }
            set { SetProperty(ref customer, value); }
        }

        private List<ProcessEntity> processes;
        public List<ProcessEntity> Processes
        {
            get { return processes; }
            set { SetProperty(ref processes, value); }
        }

        private ProcessEntity process;
        public ProcessEntity Process
        {
            get { return process; }
            set { SetProperty(ref process, value); }
        }

        public List<string> SexTypes { get; set; } = new List<string>() 
        { 
            SexType.保密.ToString(),
            SexType.男.ToString(), 
            SexType.女.ToString()
        };

        private string sex="保密";
        public string Sex
        {
            get { return sex; }
            set { SetProperty(ref sex, value); }
        }

        public ICommand LoadedCommand { get; }
        public ICommand UnloadedCommand { get; }
        public ICommand UpdateProfileCommand { get; }
        public ICommand SaveCustomerCommand { get; }
        public ICommand NavigationToCustomerViewCommand { get; }
        public ICommand CreateProcessCommand { get; }
        public ICommand DeleteProcessCommand { get; }   

        public ISession Session { get; }
        private ICustomerRepository CustomerRepository { get; }
        private IProcessRepository ProcessRepository { get; }

        public ProcessViewModel(ISession session, ICustomerRepository customerRepository, IProcessRepository processRepository)
        {
            Session = session;
            CustomerRepository = customerRepository;
            ProcessRepository = processRepository;
            LoadedCommand = new RelayCommand(OnLoadedCommand);
            UnloadedCommand = new RelayCommand(OnUnloadedCommand);
            UpdateProfileCommand = new RelayCommand(OnUpdateProfileCommand);
            SaveCustomerCommand = new RelayCommand(OnSaveCustomerCommand);
            NavigationToCustomerViewCommand = new RelayCommand(OnNavigationToCustomerViewCommand);
            CreateProcessCommand = new RelayCommand(OnCreateProcessCommand);
            DeleteProcessCommand = new RelayCommand<ProcessEntity>(OnDeleteProcessCommand);

        }

        private void OnDeleteProcessCommand(ProcessEntity entity)
        {
            var result = Session.Show("是否执行当前操作？", System.Windows.MessageBoxButton.YesNo);
            if (result == null || result.Value == false)
                return;
            int count = ProcessRepository.Delete(entity);
            if (count > 0)
            {
                LoadProcesses();
            }
        }

        private void OnCreateProcessCommand()
        {
            CreateProcess();
            LoadProcesses();
        }
        

        /// <summary>
        /// 返回至客户界面
        /// </summary>
        private void OnNavigationToCustomerViewCommand()
        {
            EventBus.Default.Publish(this, new CustomerMessage(null));
        }

        /// <summary>
        /// 保存当前客户
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void OnSaveCustomerCommand()
        {
            CustomerRepository.Update(Customer);
            ProcessRepository.SaveChanged();
        }

        /// <summary>
        /// 上传客户的头像
        /// </summary>
        private void OnUpdateProfileCommand()
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //打开的文件选择对话框上的标题
            openFileDialog.Title = "请选择文件";
            //设置文件类型
            openFileDialog.Filter = "图片文件(*.jpg)|*.jpg|图片文件(*.png)|*.png|所有文件(*.*)|*.*";
            //设置默认文件类型显示顺序
            openFileDialog.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录
            openFileDialog.RestoreDirectory = true;
            //设置是否允许多选
            openFileDialog.Multiselect = false;
            //按下确定选择的按钮
            if (openFileDialog.ShowDialog().Value == true)
            {
                //获得文件路径
                string fileName = openFileDialog.FileName;
                string extension = Path.GetExtension(fileName);
                BitmapImage ImageSource = new BitmapImage(new Uri(fileName));
                string content = ImageHelper.GetImageString(fileName);
                if (Customer != null)
                {
                    Customer.Profile = content;
                    var temp = Customer;
                    Customer = null;
                    Customer = temp;
                }                
            }
        }

        private void OnUnloadedCommand()
        {
            ProcessRepository.SaveChanged();

        }

        private void OnLoadedCommand()
        {
            LoadProcesses();
        }

        /// <summary>
        /// 创建新项目
        /// </summary>
        private void CreateProcess()
        {
            var entity = new ProcessEntity
            {
                InsertDate = DateTime.Now,
                CustomerId = Customer.Id,
                Content = "请在这里输入内容...",
            };
            int count = ProcessRepository.Insert(entity);
            if (count > 0)
            {
                Customer.UpdateDate = entity.InsertDate;
                count = CustomerRepository.Update(Customer);
                LoadProcesses();
            }
        }

        /// <summary>
        /// 加载当前客户所有项目 
        /// </summary>
        private void LoadProcesses()
        {
            Processes = ProcessRepository.GetAll()
                .FindAll(t => t.CustomerId == Customer.Id)
                .OrderByDescending(t => t.InsertDate)
                .ToList();
        }
    }
}
